
package de.midnightdance.admin.ws.entities;

import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

import de.cbf.util.ui.editentities.interfaces.IEditableEntity;
import de.midnightdance.admin.ws.entities.UuidHeader;


/**
 * <p>Java class for requiemUser complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="requiemUser">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="pass" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "requiemUser", propOrder = {
    "name",
    "pass"
})
public class RequiemUser extends UuidHeader implements IEditableEntity{
	public static final String PERMISSION_READ_ALL 	= "readAll";
	public static final String PERMISSION_WRITE_ALL = "writeAll";
	public static final String PERMISSION_ADMIN		= "admin";
	
    public static final int PCE_INDEX_Name			= 1;
	public static final int PCE_INDEX_pass 			= 2;
	public static final int PCE_INDEX_permissions	= 3;	
	
	protected String name;
    protected String pass;
    protected List<String> permissions;

    protected PropertyChangeSupport propertyChangeSupport;
        
    public RequiemUser() {
		super();
		//
		permissions = new ArrayList<String>();
		//
		propertyChangeSupport = new PropertyChangeSupport(this);
	}
    @Override
	public IEditableEntity cloneEntity() {
		RequiemUser newUser = new RequiemUser();
		newUser.takeOverEntityData(this);
		return newUser;
	}

	@Override
	public PropertyChangeSupport getPropertyChangeSupport() {
		return propertyChangeSupport;
	}

	protected void takeOverEntityData(Object aOther) {
		if (aOther instanceof RequiemUser) {
			RequiemUser otherUser = (RequiemUser) aOther;			
			this.name			= otherUser.name;
			this.pass			= otherUser.pass;			
			this.permissions.clear();
			this.permissions.addAll(otherUser.permissions);
		}
	}
	@Override
	public void takeOverValuesFrom(IEditableEntity entity)
			throws IllegalArgumentException {
		if (entity instanceof RequiemUser) {
			RequiemUser otherUser = (RequiemUser) entity;
			takeOverEntityData(otherUser);
		}else{
			throw new IllegalArgumentException("Expected: "+RequiemUser.class+", Got: "+entity.getClass());
		}		
	}
	/**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {    
        if(value == null || value.equals(name))return;
		String oldName = name;
		name = value;
		propertyChangeSupport.fireIndexedPropertyChange("Name", PCE_INDEX_Name, oldName, name);
    }
    /**
     * Gets the value of the pass property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getPass() {
        return pass;
    }

    /**
     * Sets the value of the pass property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setPass(String value) {
       if(value == null || value.equals(pass))return;
		String oldpass = pass;
		pass = value;
		propertyChangeSupport.fireIndexedPropertyChange("pass", PCE_INDEX_pass, oldpass, pass); 
    }

    public Iterator<String> iteratorPermissions() {
		return permissions.iterator();
	}
	public void setPermissions(Collection<String> value ) {
		if(this.permissions != null && this.permissions.equals(value)){ return; }		
		//
		this.permissions.clear();
		//
		if(value == null){
			return;
		}
		boolean change = this.permissions.addAll(value);		
		if(change){			
			propertyChangeSupport.fireIndexedPropertyChange(
					"permissions",PCE_INDEX_permissions, null,this.permissions);			
		}		
	}
	public boolean addToPermissions(String permission){	
		boolean change = permissions.add(permission);
		if(change){			
			propertyChangeSupport.fireIndexedPropertyChange(
					"permissions",PCE_INDEX_permissions, null,this.permissions);
		}
		return change;		
	}
	public boolean removeFromPermissions(String permission) {
		boolean change = permissions.remove(permission); 
		if(change){			
			propertyChangeSupport.fireIndexedPropertyChange(
					"permissions",PCE_INDEX_permissions, null,this.permissions);
		}
		return change;
	}  

	

}
